<template>
	<view class="note_box">
		<view class="note_content">
			<text>我的城市</text>
			<!-- <u-cell-group> -->
			<u-cell size="large" :title="city" isLink @click="showPicker()" style="margin-bottom: 30rpx;"></u-cell>
			<!-- </u-cell-group> -->
			<u-picker :show="show" ref="uPicker" :columns="columns" @confirm="confirm" @change="changeHandler">
			</u-picker>
			<text>我的标题</text>
			<view class="note__title">
				<u--input placeholder="请输入标题" border="bottom" v-model="noteList.note_title" clearable></u--input>
			</view>
			<text>我的笔记</text>
			<view class="uni-textarea">
				<textarea maxlength='-1' v-model="noteList.content" />
			</view>
			<view class="u-demo-block">
				<text class="u-demo-block__title">添加图片</text>
				<view class="u-demo-block__content">
					<view class="u-page__upload-item">
						<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
							:maxCount="10" :previewFullImage="true"></u-upload>
					</view>
				</view>
			</view>
			<view style="height: 130rpx;">
			</view>
		</view>
		<view class="btn_send">
			<u-button color="#cde6c7" text="发表" @click="sendNote()"></u-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				show: false,
				focusTextarea: '',
				focus: false,
				city: '广东广州',
				columns: [
					['广东'],
					['广州', '深圳', '珠海', '佛山', '韶关', '湛江', '茂名', '惠州', '汕头', '潮州', '其他']
				],
				fileList1: [],
				noteList: {
					region: "广州",
					note_title: "",
					content: "",
					images: [],
					uid:uni.getStorageSync('token')
				}
			}
		},
		methods: {
			changeHandler(e) {
				const {
					columnIndex,
					value,
					values, // values为当前变化列的数组内容
					index,
					// 微信小程序无法将picker实例传出来，只能通过ref操作
					picker = this.$refs.uPicker
				} = e
				// 当第一列值发生变化时，变化第二列(后一列)对应的选项
				if (columnIndex === 0) {
					// picker为选择器this实例，变化第二列对应的选项
					picker.setColumnValues(1, this.columnData[index])

				}
			},
			// 回调参数为包含columnIndex、value、values
			confirm(e) {
				this.show = false
				this.city = e.value[0] + e.value[1]
				this.noteList.region = e.value[1]
			},
			showPicker(index) {
				//this.index = index + 1
				this.show = true
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
				this.noteList.images.splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {	
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
					this.noteList.images.push({
						src:lists[i].name
					})
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'http://localhost:8081/minioO/addImage', // 上传的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
						}
					});
				})
			},
			sendNote() {
				uni.request({
					url: 'http://localhost:8081/note/addNote',
					method: 'POST',
					data: this.noteList,
					header: {
						'Content-Type': 'application/json',
					},
					dataType: 'json',
					success: (res) => {
						if (res.data.state == 200) {
							uni.showToast({
								title: '发表成功',
								duration: 1000,
							});
						} else {
							uni.$u.toast(res.data.message)
						}
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	.note_box {
		margin: 50rpx 30rpx;

		.note__content {
			padding-bottom: 200rpx;

			text {
				font-size: 40rpx;
			}

			.note__title {
				margin-bottom: 30rpx;
			}
		}

		.uni-textarea {
			margin-top: 20rpx;
			padding: 15rpx;
			border: 2rpx solid #f3f3f4;

			textarea {
				height: 500rpx;
			}
		}

		.btn_send {
			width: 90%;
			margin: auto;
			position: fixed;
			bottom: 40rpx;
			left: 0;
			right: 0;
			z-index: 1;
		}
	}
</style>
